//
//  BaccListViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/15.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BaccListViewController.h"
#import "Bacclistmodel.h"
#import "BacclistTableViewCell.h"
#import "BillDetailViewController.h"

@interface BaccListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;

@end

@implementation BaccListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F2F2"];
    self.title = @"结算记录";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    
    self.mydateSource = [NSMutableArray arrayWithCapacity:1];
    [self.myTableView reloadData];
    
    [self request];
    [self showheader];
    // Do any additional setup after loading the view.
}

-(void)showheader{
    UIView *head = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 184)];
    UILabel *contertLb = [[UILabel alloc]initWithFrame:CGRectMake(0, 178, KWIDTH, 16)];
    contertLb.textAlignment = NSTextAlignmentCenter;
    contertLb.textColor = [UIColor colorWithHexString:@"#979797"];
    contertLb.font = FontSize(11);
    contertLb.text = [NSString stringWithFormat:@"一  仅显示一年内的结算历史  一"];
    [head addSubview:contertLb];
    self.myTableView.tableFooterView = head;
}

-(void)request{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    [NetWorkTool POST:agentSeetHisList param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        
        self.mydateSource = [Bacclistmodel mj_objectArrayWithKeyValuesArray:dic[@"data"]];
        
        [self.myTableView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        
    } fail:^(NSError *error) {
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
    
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.mydateSource.count;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 2;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 75;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BacclistTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BacclistTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Bacclistmodel *model = [_mydateSource safeObjectAtIndex:indexPath.section];
    [cell refash:model];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    Bacclistmodel *model = [_mydateSource safeObjectAtIndex:indexPath.section];
    BillDetailViewController *VC = [BillDetailViewController new];
    VC.bacModel = model;
    VC.from = @"2";
    [self.navigationController pushViewController:VC animated:YES];

}


-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStylePlain)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        _myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
            [_myTableView.mj_header beginRefreshing];
        }];
        
        [_myTableView registerNib:[UINib nibWithNibName:@"BacclistTableViewCell" bundle:nil] forCellReuseIdentifier:@"BacclistTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end
